package com.aube.plugin.gallery;

import com.aube.plugin.gallery.preview.FavorManager;
import com.huyn.baseframework.model.Picture;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * Created by huyaonan on 16/9/18.
 */
public class PictureImp extends Picture {

    public String picid;
    public int praiseCount;
    public int commentCount;
    public String status;

    public boolean isPraised() {
        return "Y".equalsIgnoreCase(status);
    }

    public static List<PictureImp> parse(List<Object> list) {
        List<PictureImp> pics = new ArrayList<>();
        PictureImp picture;
        for(Object obj : list) {
            picture = new PictureImp();
            LinkedHashMap<String, Object> lhm = (LinkedHashMap<String, Object>) obj;
            Iterator<String> keyIterator = lhm.keySet().iterator();
            while (keyIterator.hasNext()) {
                String key = keyIterator.next();
                Object value = lhm.get(key);

                if("picurl".equalsIgnoreCase(key)) {
                    picture.setPictureUrl((String) value);
                } else if("picid".equalsIgnoreCase(key)) {
                    picture.picid = (String) value;
                } else if("status".equalsIgnoreCase(key)) {
                    picture.status = (String) value;
                } else if("width".equalsIgnoreCase(key)) {
                    picture.setWidth((int)((double) value));
                } else if("height".equalsIgnoreCase(key)) {
                    picture.setHeight((int)((double) value));
                } else if("praiseCount".equalsIgnoreCase(key)) {
                    picture.praiseCount = (int)((double) value);
                } else if("commentCount".equalsIgnoreCase(key)) {
                    picture.commentCount = (int)((double) value);
                } else if("picdesc".equalsIgnoreCase(key)) {
                    if(value != null)
                        picture.setDesc((String) value);
                }
            }

            FavorManager.getInstance().update(picture.picid, picture.isPraised());
            FavorManager.getInstance().addFavorCount(picture.picid, picture.praiseCount);
            FavorManager.getInstance().addCommentCount(picture.picid, picture.commentCount);

            pics.add(picture);
        }
        return pics;
    }

}
